﻿using Authentication.Configuration;
using Common.Cache;
using Common.Contract;
using Common.Contract.Customer;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request;
using Common.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Authentication.Web.Controller
{
    public static class Helper
    {
        private static List<SelectListItem> shopTypes = null;
        public static List<SelectListItem> ShopTypes
        {
            get
            {
                if (shopTypes != null) return shopTypes;
                shopTypes = new List<SelectListItem>();
                shopTypes.Add(new SelectListItem { Value = "SUB-D", Text = "Sub Dealer" });
                shopTypes.Add(new SelectListItem { Value = "MC", Text = "Modern Channel" });
                shopTypes.Add(new SelectListItem { Value = "DSP", Text = "Direct Sale Point" });
                shopTypes.Add(new SelectListItem { Value = "WS", Text = "Whole Sale" });
                return shopTypes;
            }
        }

        private static List<SelectListItem> channels = null;
        public static List<SelectListItem> Channels
        {
            get
            {
                if (channels != null) return channels;
                channels = new List<SelectListItem>();
                channels.Add(new SelectListItem { Value = "Y", Text = "Direct" });
                channels.Add(new SelectListItem { Value = "N", Text = "In-Direct" });
                return channels;
            }
        }

        private static List<SelectListItem> flags = null;
        public static List<SelectListItem> Flags
        {
            get
            {
                if (flags != null) return flags;
                flags = new List<SelectListItem>();
                flags.Add(new SelectListItem { Value = "Y", Text = "Y" });
                flags.Add(new SelectListItem { Value = "N", Text = "N" });
                return flags;
            }
        }

        public static List<RegionRecord> Regions
        {
            get
            {
                return CacheFactory.MemCache.GetFromCache<List<RegionRecord>>(CACHING_NAME.LIST_ALL_REGIONS, () =>
                {
                    using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
                    {
                        SearchRegionsRequest request = new SearchRegionsRequest
                        {
                            Header = new RequestHeaderRecord
                            {
                                ApplicationKey = SystemConfiguration.ApplicationKey,
                                CallerName = "Authentication.Web.Controller.Helper"
                            }
                        };
                        var result = serviceClient.Proxy.SearchRegions(request);
                        if (result.IsSuccess)
                        {
                            return result.Records;
                        }
                        return null;
                    }
                }, SystemConfiguration.DefaultCacheTime);
            }
        }

        public static List<ProvinceRecord> Provinces
        {
            get
            {
                return CacheFactory.MemCache.GetFromCache<List<ProvinceRecord>>(CACHING_NAME.LIST_ALL_PROVINCES, () =>
                {
                    using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
                    {
                        SearchProvincesRequest request = new SearchProvincesRequest
                        {
                            Header = new RequestHeaderRecord
                            {
                                ApplicationKey = SystemConfiguration.ApplicationKey,
                                CallerName = "Authentication.Web.Controller.Helper"
                            }
                        };
                        var result = serviceClient.Proxy.SearchProvinces(request);
                        if (result.IsSuccess)
                        {
                            return result.Records;
                        }
                        return null;
                    }
                }, SystemConfiguration.DefaultCacheTime);
            }
        }

        public static List<DistrictRecord> Districts
        {
            get
            {
                return CacheFactory.MemCache.GetFromCache<List<DistrictRecord>>(CACHING_NAME.LIST_ALL_DISTRICTS, () =>
                {
                    using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
                    {
                        SearchDistrictsRequest request = new SearchDistrictsRequest
                        {
                            Header = new RequestHeaderRecord
                            {
                                ApplicationKey = SystemConfiguration.ApplicationKey,
                                CallerName = "Authentication.Web.Controller.Helper"
                            }
                        };
                        var result = serviceClient.Proxy.SearchDistricts(request);
                        if (result.IsSuccess)
                        {
                            return result.Records;
                        }
                        return null;
                    }
                }, SystemConfiguration.DefaultCacheTime);
            }
        }
    }
}